#include "stdafx.h"
#include "XHotKeyImpl.h"

#include "Util.h"
#include "UIUtil.h"

#include "IntSkinImpl.h"
#include "XBSTR.h"

template <class I>
CXHotKeyImpl<I>::CXHotKeyImpl()
{
    ModifyStyle(WS_TABSTOP, 0);
}

template <class I>
CXHotKeyImpl<I>::~CXHotKeyImpl()
{
}

//////////////////////////////////////////////////////////////////////////
// IXHotKey

template <class I>
BOOL CXHotKeyImpl<I>::GetHotKey(WORD &wVirtualKeyCode, WORD &wModifiers)
{
    wVirtualKeyCode = wModifiers = 0;
    if(m_xRealWnd != NULL)
    {
        DWORD dwCode = ::SendMessage((HWND)m_xRealWnd, HKM_GETHOTKEY, 0, 0);
        wVirtualKeyCode = LOBYTE(LOWORD(dwCode));
        if(HIBYTE(LOWORD(dwCode)) & MOD_ALT)
        {
            wModifiers |= MOD_SHIFT;
        }
        if(HIBYTE(LOWORD(dwCode)) & MOD_CONTROL)
        {
            wModifiers |= MOD_CONTROL;
        }
        if(HIBYTE(LOWORD(dwCode)) & MOD_SHIFT)
        {
            wModifiers |= MOD_ALT;
        }
        return TRUE;
    }
    return FALSE;
}

template <class I>
BOOL CXHotKeyImpl<I>::SetHotKey(WORD wVirtualKeyCode, WORD wModifiers)
{
    if(m_xRealWnd == NULL)
        return FALSE;

    WORD modifier = 0;
    if(wModifiers& MOD_ALT)
    {
        modifier |= MOD_SHIFT;
    }
    if(wModifiers& MOD_CONTROL)
    {
        modifier |= MOD_CONTROL;
    }
    if(wModifiers& MOD_SHIFT)
    {
        modifier |= MOD_ALT;
    }
    ::SendMessage((HWND)m_xRealWnd, HKM_SETHOTKEY, MAKEWORD(wVirtualKeyCode, modifier), 0L); 
    return TRUE;
}

template <class I>
XString CXHotKeyImpl<I>::GetHotKeyName()
{
    XString strKeyName;
    WORD wCode = 0;
    WORD wModifiers = 0;

    if(!GetHotKey(wCode, wModifiers))
        return strKeyName;

    return Util::GetHotKeyName(wCode, wModifiers);
}

template <class I>
BOOL CXHotKeyImpl<I>::SetRules(WORD wInvalidComb, WORD wModifiers)
{
    if(m_xRealWnd == NULL)
        return FALSE;

    ::SendMessage((HWND)m_xRealWnd, HKM_SETRULES, wInvalidComb, MAKELPARAM(wModifiers, 0));
    return TRUE;
}

//////////////////////////////////////////////////////////////////////////
// IXBaseObj
template <class I>
void CXHotKeyImpl<I>::OnFinalRelease()
{
    superclass::OnFinalRelease();
}

//////////////////////////////////////////////////////////////////////////
// CXHwndImpl Callback
template <class I>
XString CXHotKeyImpl<I>::GetHwndClassName()
{
    return XString(HOTKEY_CLASS);
}

// CXControlImpl
template <class I>
HRESULT CXHotKeyImpl<I>::OnSkinLangChanged()
{
    return superclass::OnSkinLangChanged();
}
//////////////////////////////////////////////////////////////////////////

template <class I>
DWORD CXHotKeyImpl<I>::GetHwndFlags()
{
    return XHwnd_NeedCommand;
}

template <class I>
BOOL CXHotKeyImpl<I>::OnCommand(UINT command)
{
    LPCTSTR szEvt = NULL;
    if(command == EN_CHANGE)
        szEvt = HotKeyEvent_Change;

    if(szEvt != NULL)
    {
        InvokeEventHandler(this, szEvt, (LPARAM)m_xRealWnd);
        return TRUE;
    }

    return FALSE;
}

template <class I>
HRESULT CXHotKeyImpl<I>::get_Text(IN BSTR* bstrText)
{
    XString strText = GetHotKeyName();
    if(::SysReAllocString(bstrText, strText.GetData()))
        return S_OK;
    else
        return E_FAIL;
}
